package com.cabrida.livestar.mainstar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.model.StarMainData;
import com.cabrida.livestar.util.MyUtils;

/**
 * 明星首页列表adapter
 * 
 * @author why
 * 
 */
public class StarHomePageLvAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<StarMainData> data_list = new ArrayList<StarMainData>();

	public StarHomePageLvAdapter(Context context) {
		this.mInflater = LayoutInflater.from(context);
	}

	public void setData(ArrayList<StarMainData> infoList) {
		data_list = infoList;
	}

	@Override
	public int getCount() {
		return data_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {

			holder = new ViewHolder();

			convertView = mInflater.inflate(
					R.layout.adapter_star_homepage_lv_item, null);
			convertView.setTag(holder);
			holder.tv_left = (TextView) convertView.findViewById(R.id.tv_left);
			holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.ll_redpacket = (LinearLayout) convertView.findViewById(R.id.ll_redpacket);
			holder.ll_gift = (LinearLayout) convertView.findViewById(R.id.ll_gift);
			holder.view_line_top = (View) convertView.findViewById(R.id.view_line_top);
			holder.view_line_bottom = (View) convertView.findViewById(R.id.view_line_bottom);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		if (position==0) {
			holder.view_line_top.setVisibility(View.INVISIBLE);
			if (position==data_list.size()-1) {
				holder.view_line_bottom.setVisibility(View.INVISIBLE);
			}else {
				holder.view_line_bottom.setVisibility(View.VISIBLE);
			}
		}else {
			holder.view_line_top.setVisibility(View.VISIBLE);
			if (position==data_list.size()-1) {
				holder.view_line_bottom.setVisibility(View.INVISIBLE);
			}else {
				holder.view_line_bottom.setVisibility(View.VISIBLE);
			}
		}
		holder.tv_date.setText(data_list.get(position).getL_date().toString());
		holder.tv_time.setText(data_list.get(position).getL_startime().toString());
		if ("0".equals(data_list.get(position).getL_package())) {
			holder.ll_redpacket.setVisibility(View.GONE);
		}else {
			holder.ll_redpacket.setVisibility(View.VISIBLE);
		}
		if ("0".equals(data_list.get(position).getL_gift())) {
			holder.ll_gift.setVisibility(View.GONE);
		}else {
			holder.ll_gift.setVisibility(View.VISIBLE);
		}
		
		try {
			if (MyUtils.IsToday(data_list.get(position).getL_date())) {
				holder.tv_left.setText("今天");
			}else if (MyUtils.IsTomorrow(data_list.get(position).getL_date())) {
				holder.tv_left.setText("明天");
			}else {
				holder.tv_left.setText("");
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return convertView;
	}

	class ViewHolder {
		private TextView tv_left;
		private TextView tv_date;
		private TextView tv_time;
		private LinearLayout ll_redpacket;
		private LinearLayout ll_gift;
		private TextView tv_gift;
		private View view_line_top;
		private View view_line_bottom;
	}

}
